package ExerChapter14String_;

// 写一个注册程序要求用户名2-4位，密码6为且需全为数字，邮箱包含@和.且.在@前面
import java.util.Scanner;

public class Hwork2 {
    public static void main(String[] args) {
        try {
            registied();
        } catch (RuntimeException e) {
            // TODO: handle exception
            System.out.println(e.getMessage());
            
        }
        
    }

    public static void registied(){

        System.out.println("请输入:\n用户名 密码 邮箱！");
        Scanner foo = new Scanner(System.in);

        String[] s = foo.nextLine().split(" ");
    
        String s1 = s[0];

        if (!(s1.length() >= 2 && s1.length() <=4)) {
            throw new RuntimeException("用户名需为2-4位");
        }
    
        String s2 = s[1];

        if(s2.length() != 6 && isDigital(s2)){
            throw new NumberFormatException("必须为数字");
        }
    
        String s3 = s[2];

        int i = s3.indexOf('@');
        int j = s3.indexOf(".");

        if(!(i >= 0 && i < j)){
            throw new RuntimeException("必须包含@和.且.在@后");
        }

        System.out.println("恭喜注册成功！\n用户名为：" +  s1 + "\n密码为：" + s2 + "\n邮箱为：" + s3);

    }

    public static Boolean isDigital(String s){
        char[] c = s.toCharArray();
        for (int i = 0; i < c.length; i++) {
            if (i >= '0' && i <= '9') {
                return true;
            }
        }
        return false;
    }
}
